﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using TravelSite.Entities;
using System.Web.UI;
using System.ComponentModel;

namespace TravelSite.Code
{
    public class BaseBookingPage : BaseDataPage
    {
        protected bool IsEmptyGrid { get; set; }

        protected IEnumerable<Contact> GridData
        {
            get
            {
                if (this.Session[PageName + "_GridData"] != null)
                    return (IEnumerable<Contact>)this.Session[PageName + "_GridData"];
                else
                    return null;
            }
            set
            {
                this.Session[PageName + "_GridData"] = value;
            }
        }

        protected List<Contact> GridViewData
        {
            get
            {
                var gridData = GridData;

                foreach (KeyValuePair<string, string> f in FilterData)
                {
                    if (IsTextFilter(f.Key))
                    {
                        PropertyDescriptor prop = TypeDescriptor.GetProperties(typeof(Contact)).Find(f.Key, false);
                        gridData = gridData.Where(x => (prop.GetValue(x)??"").ToString().ToLower().Contains(f.Value.ToLower())).ToList();
                    }
                }

                if (!string.IsNullOrEmpty(SortExpression))
                {
                    string[] sortData = ParseSortExpression();
                    if (sortData[0] == "BankInfoDate")
                    {
                        if (sortData[1] == "asc")
                            gridData = gridData.OrderBy(x => x.BankInfoDate);
                        else
                            gridData = gridData.OrderByDescending(x => x.BankInfoDate);
                    }
                    else if (sortData[0] == "TourDateDate")
                    {
                        if (sortData[1] == "asc")
                            gridData = gridData.OrderBy(x => x.TourDateDate);
                        else
                            gridData = gridData.OrderByDescending(x => x.TourDateDate);
                    }
                    else if (sortData[0] == "HotelbedsBookedDate")
                    {
                        if (sortData[1] == "asc")
                            gridData = gridData.OrderBy(x => x.HotelbedsBookedDate);
                        else
                            gridData = gridData.OrderByDescending(x => x.HotelbedsBookedDate);
                    }
                    else if (sortData[0] == "HotelbedsArrivalDate")
                    {
                        if (sortData[1] == "asc")
                            gridData = gridData.OrderBy(x => x.HotelbedsArrivalDate);
                        else
                            gridData = gridData.OrderByDescending(x => x.HotelbedsArrivalDate);
                    }
                    else if (sortData[0] == "HotelbedsDepartureDate")
                    {
                        if (sortData[1] == "asc")
                            gridData = gridData.OrderBy(x => x.HotelbedsDepartureDate);
                        else
                            gridData = gridData.OrderByDescending(x => x.HotelbedsDepartureDate);
                    }
                    else if (sortData[0] == "HotelbedsPaymentDate")
                    {
                        if (sortData[1] == "asc")
                            gridData = gridData.OrderBy(x => x.HotelbedsPaymentDate);
                        else
                            gridData = gridData.OrderByDescending(x => x.HotelbedsPaymentDate);
                    }
                    else if (sortData[0] == "HotelbedsExpoPaidDate")
                    {
                        if (sortData[1] == "asc")
                            gridData = gridData.OrderBy(x => x.HotelbedsExpoPaidDate);
                        else
                            gridData = gridData.OrderByDescending(x => x.HotelbedsExpoPaidDate);
                    }
                    else if (sortData[0] == "HotelbedsHBPaidDate")
                    {
                        if (sortData[1] == "asc")
                            gridData = gridData.OrderBy(x => x.HotelbedsHBPaidDate);
                        else
                            gridData = gridData.OrderByDescending(x => x.HotelbedsHBPaidDate);
                    }
                }

                if (gridData.Any())
                {
                    IsEmptyGrid = false;
                    return gridData.ToList();
                }
                else
                {
                    IsEmptyGrid = true;
                    List<Contact> emptyList = new List<Contact>();
                    emptyList.Add(new Contact());
                    return emptyList;
                }
                
            }
        }

        protected List<KeyValuePair<string, string>> FilterData
        {
            get
            {
                if (this.Session[PageName + "_FilterData"] == null)
                    this.Session[PageName + "_FilterData"] = new List<KeyValuePair<string, string>>();

                return (List<KeyValuePair<string, string>>)this.Session[PageName + "_FilterData"];
            }
        }

        protected bool IsTextFilter(string dataField)
        {
            return true;
        }

        protected void grid_RowCreated(Object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                GridViewRow row = e.Row;
                foreach (TableCell cell in row.Cells)
                {
                    int i = 0;
                    foreach (Control ctrl in cell.Controls)
                    {
                        if (ctrl is LinkButton)
                        {
                            LinkButton lb = (LinkButton)ctrl;
                            lb.CssClass = "sort_button";
                            Image sortIcon = GetSortIcon(lb.CommandArgument);
                            if (sortIcon != null)
                            {
                                cell.Controls.AddAt(i + 1, sortIcon);
                                break;
                            }
                        }

                        ++i;
                    }
                }

            }
        }

        protected virtual void grid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                GridViewRow row = e.Row;
                for (int i = 0; i < row.Cells.Count; ++i)
                {
                    if ((row.Cells[i].Controls.Count >= 4) && (row.Cells[i].Controls[3] is TextBox))
                    {
                        TextBox tb = (TextBox)row.Cells[i].Controls[3];
                        if (tb != null)
                        {
                            string dataField = tb.Attributes["data-field"];
                            KeyValuePair<string, string> existsFilter = FilterData.Find(item => item.Key.Equals(dataField));
                            if (!existsFilter.Equals(default(KeyValuePair<string, string>)))
                            {
                                tb.Text = existsFilter.Value;
                            }
                        }
                    }
                }
            }
            
        }

        protected void Filter_TextChanged(Object sender, EventArgs e)
        {
            TextBox tb = sender as TextBox;

            string dataField = tb.Attributes["data-field"];
            string selectedText = tb.Text;

            KeyValuePair<string, string> existsFilter = FilterData.Find(i => i.Key.Equals(dataField));
            if (!existsFilter.Equals(default(KeyValuePair<string, string>)))
                FilterData.Remove(existsFilter);

            if (!string.IsNullOrEmpty(selectedText))
                FilterData.Add(new KeyValuePair<string, string>(dataField, selectedText));

            LoadData();
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "focus", "$(document).ready(function () { $('#" + tb.ClientID + "').putCursorAtEnd();  });", true);
        }
    }
}